"""
    封装汽车出租模块
"""
from car_rental.baw import login
from car_rental.baw import rent
from car_rental.caw.base_request import BaseRequest


def check_rent_exist(url, test_request, cs):
    """
    初始化租车单
    :param url: 测试环境
    :param test_request: BaseRequests的实例
    :param cs: 测试数据
    :return: 检查租出单是否存在口的响应
    """
    url = url + "/carRental/check/checkRentExist.action"
    r = test_request.post(url, data=cs)
    return r


def init_check_form_data(url, test_request, cs):
    """
    保存租车单
    :param url: 测试环境
    :param test_request: BaseRequests的实例
    :param cs: 测试数据
    :return: 初始化检查单接口的响应
    """
    url = url + "/carRental/check/initCheckFormData.action"
    hd = {
        "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"
    }
    r = test_request.post(url, headers=hd, data=cs)
    return r


def save_check(url, test_request, cs):
    """
    保存租车单
    :param url: 测试环境
    :param test_request: BaseRequests的实例
    :param cs: 测试数据
    :return: 初始化检查单接口的响应
    """
    url = url + "/carRental/check/saveCheck.action"
    hd = {
        "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"
    }
    r = test_request.post(url, headers=hd, data=cs)
    return r


if __name__ == '__main__':
    url = "http://192.168.151.7:8080"
    test_request = BaseRequest()
    login.login(url, test_request, {"loginname": "admin", "pwd": "123456"})
    #   测试初始化接口
    init_cs = {
        "identity": "3604206242316",
        "price": 3500,
        "carnumber": "赣G1563R9"
    }
    r = rent.init_rent_form(url, test_request, init_cs)
    print(r)
    rentid = r.json()["rentid"]
    print(rentid)
    begindate = r.json()["begindate"]
    opernam = r.json()["opername"]
    #   测试保存接口
    save_cs = {
        "rentid": rentid,
        "begindate": begindate,
        "returndate": "2022-05-14 00:00:00",
        "identity": init_cs["identity"],
        "carnumber": init_cs["carnumber"],
        "price": init_cs["price"],
        "opername": opernam
    }
    rent.save_Rent(url, test_request, save_cs)
    print(check_rent_exist(url, test_request, {"rentid": rentid}))
    r = init_check_form_data(url, test_request,{"rentid":rentid})
    print(r)
    checkid =r.json()["check"]["checkid"]
    print(checkid)
    save_cs={
        "checkid": checkid,
        "rentid":rentid,
        "opername": opernam,
        "problem": "无",
        "paymoney": 0,
        "checkdate": "2022-05-14 00:00:00",
        "checkdesc": "无"
    }
    r = save_check(url, test_request, save_cs)
    print(r)

